package com.stone.sys.action;

/**
 * <pre>
 * 字典信息维护
 * @author create
 * </pre>
 */

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.sys.pojo.TCydy;
import com.stone.sys.pojo.TCydyType;
import com.stone.sys.pojo.TDicType;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.JsonUtils;
import com.stone.utils.Resources;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class DictionaryAction extends BaseAction {

	private static final long serialVersionUID = -4647594073681054487L;

	private DictionaryService dicService;

	/**
	 * 字典
	 */
	private TDictionary dic;

	private TDicType dictype;

	/**
	 * ID
	 */
	private String id;

	/**
	 * 字典列表
	 */
	private List<TDictionary> dicList;

	private String pagetype;
	/**
	 * 常用短语
	 */
	private TCydy cydy;
	private TCydyType cydytype;
	
	/**
	 * 常用短语列表
	 */
	private List<TCydy> cydyList;
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		// 常用短语类型
		if ("cydytype".equals(this.pagetype)) {
			
			// 修改进入
			 if (!"0".equals(id)) {
				
				this.cydytype = (TCydyType)dicService.findById(Long.parseLong(id), TCydyType.class.getName());
	
				// 新建进入
			} else {
				this.cydytype = new TCydyType();
				this.cydytype.setId(Long.parseLong("0"));
				//this.dic.setItemType(super.getSearchBean().getCon1());
		
			}
	
			return "CYDY_TYPE_SUCCESS";
			
			
			// 常用短语
		} else if ("cydy".equals(this.pagetype)) {
			
			// 修改进入
			 if (!"0".equals(id)) {
				
				this.cydy = (TCydy)dicService.findById(Long.parseLong(id), TCydy.class.getName());
	
				// 新建进入
			} else {
				//this.dic = new TDictionary();
				this.cydy.setId(Long.parseLong("0"));
				//this.dic.setItemType(super.getSearchBean().getCon1());
		
			}
	
			return "CYDY_SUCCESS";
			
			
			// 字典类型
		} else if ("dictype".equals(this.pagetype)) {
			

			// 修改进入
			 if (!"0".equals(id)) {
				
				this.dictype = (TDicType)dicService.findById(Long.parseLong(id), TDicType.class.getName());

				// 新建进入
			} else {
				this.dictype = new TDicType();
				this.dictype.setId(Long.parseLong("0"));
				//this.dic.setItemType(super.getSearchBean().getCon1());
		
			}

			return "DIC_TYPE_SUCCESS";
			
			
		} else {

			// 修改进入
			 if (!"0".equals(id)) {
				
				this.dic = (TDictionary)dicService.findById(Long.parseLong(id), TDictionary.class.getName());
	
				// 新建进入
			} else {
				//this.dic = new TDictionary();
				this.dic.setId(Long.parseLong("0"));
				//this.dic.setItemType(super.getSearchBean().getCon1());
		
			}
	
			return SUCCESS;
		
		}
	}
		
	

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {

		// 常用短语类型
		if ("cydytype".equals(this.pagetype)) {
			
			// 新建时
			if (cydytype.getId() == 0) {
				dicService.save(cydytype);	
				// 修改排序号
				dicService.updateByHQL("update TCydyType set sortid = ? where id = ?", new Object[]{cydytype.getId(), cydytype.getId()});
			} else {

				dicService.update(cydytype);
				
			}
			super.setInfo("保存成功");
			return "CYDY_TYPE_SUCCESS";
			
			
			// 常用短语
		} else if ("cydy".equals(this.pagetype)) {
			
			// 新建时
			if (cydy.getId() == 0) {
				dicService.save(cydy);	
				// 修改排序号
				dicService.updateByHQL("update TCydy set sortid = ? where id = ?", new Object[]{cydy.getId(), cydy.getId()});
			} else {

				dicService.update(cydy);
				
			}
			super.setInfo("保存成功");
			return "CYDY_SUCCESS";
			
			
			// 字典类型
		} else if ("dictype".equals(this.pagetype)) {
			

			// 新建时
			if (dictype.getId() == 0) {
				dicService.save(dictype);	
				// 修改排序号
				dicService.updateByHQL("update TDicType set sortid = ? where id = ?", new Object[]{dictype.getId(), dictype.getId()});
			} else {

				dicService.update(dictype);
				
			}

			super.setInfo("保存成功");

			return "DIC_TYPE_SUCCESS";
			
			
		} else {

			// 新建时
			if (dic.getId() == 0) {
				dicService.save(dic);	
				// 修改排序号
				dicService.updateByHQL("update TDictionary set sortid = ? where id = ?", new Object[]{dic.getId(), dic.getId()});
			} else {

				dicService.update(dic);
				
			}

			super.setInfo("保存成功");
			
			return SUCCESS;
		
		}
		
	}
	/**
	 * 验证
	 * 
	 * @return
	 * @throws AppException
	 * @throws IOException
	 */
	public void check() throws Exception {

		String out = "{}";

		long count = 0;


		// 字典类型
		if ("dictype".equals(this.pagetype)) {
			
			count = dicService.getTotalByDHql("typecode=?", new Object[]{dictype.getTypecode()}, "TDicType");
			
			// 常用短语类型
		} else if ("cydytype".equals(this.pagetype)) {
			
			count = dicService.getTotalByDHql("typecode=?", new Object[]{cydytype.getTypecode()}, "TCydyType");
			
			// 常用短语
		} else if ("cydy".equals(this.pagetype)) {
			
			count = dicService.getTotalByDHql("dytype=? and dycode = ?", new Object[]{cydy.getDytype(),cydy.getDycode()}, "TCydy");
			
			// 字典
		} else {
		
			String itemId = super.getRequest().getParameter("itemId");
			String itemType = super.getRequest().getParameter("itemType");
			
				
			// 登陆账号验证
			count = dicService.getTotalByDHql("itemType=? and itemId = ?", new Object[]{itemType,itemId}, "TDictionary");
			
		}
		
		if (count > 0) {
			out = "{m:'编号已经存在！'}";
		}
		
		super.getResponse().getWriter().print(out);
		
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {

		// 字典类型
		if ("dictype".equals(this.pagetype)) {
			
			dicService.doDelAll(id, "TDicType");
			return "DIC_TYPE_DEL-SUCCESS";
			
		} else if ("cydy".equals(this.pagetype)) {
			
			dicService.doDelAll(id, "TCydy");
			return "CYDY_DEL-SUCCESS";
			
		} else if ("cydytype".equals(this.pagetype)) {
			dicService.doDelAll(id, "TCydyType");
			return "CYDY_TYPE_DEL-SUCCESS";
			
		} else {
		
			dicService.doDelAll(id, "TDictionary");
			
			return "DEL-SUCCESS";
		}
		
	}
	/**
	 * 进入字典弹出页面
	 */
	public String initDic() {

		dicList = dicService
				.findListByHql(" from TDictionary where itemType = '"
						+ dic.getItemType() + "' order by sortid");

		return "DIC_POPUP_SUCCESS";

	}

	/**
	 * 进入字典弹出页面
	 */
	public void getDicJson() throws Exception {

		dicList = dicService
				.findListByHql(" from TDictionary where itemType in ('"
						+ dic.getItemType() + "') order by sortid");

		if (!StringUtils.isNullOrBlank(super.getRequest().getParameter(
				"emptyFlag"))) {
			TDictionary dic = new TDictionary();
			dic.setItemName("全部");
			dicList.add(0, dic);
		}

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");

		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);

		super.getResponse().getWriter().print(json.toString());

	}
	
	/**
	 * 进入字典弹出页面
	 */
	public void getEquipDicJson() throws Exception {
		
		String where = "";
		
		// 一次设备
		if ("SBLX1".equals(dic.getItemType())) {
			
			where = " and itemId like '0%'";
			
			// 二次设备
		} else if ("SBLX2".equals(dic.getItemType())) {
			where = " and itemId like 'E%'";
		}

		dicList = dicService
				.findListByHql(" from TDictionary where itemType = 'SBLX'" + where + " order by sortid");

		if (!StringUtils.isNullOrBlank(super.getRequest().getParameter(
				"emptyFlag"))) {
			TDictionary dic = new TDictionary();
			dic.setItemName("全部");
			dicList.add(0, dic);
		}

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");

		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);

		super.getResponse().getWriter().print(json.toString());

	}
	/**
	 * 进入常用短语弹出页面
	 */
	public String initCydy() {
		
		cydy.setDytypename(Resources.getString(cydy.getDytype(), "struts"));
		
		String where = "where dytype = '" + super.getRequest().getParameter("cydy.dytype") + "'";
		
		if (!StringUtils.isNullOrBlank(this.cydy.getDyname())) {
			where += " and dyname like '%" + this.cydy.getDyname() + "%'";
		}
		
		cydyList = dicService.findListByHql(" from TCydy " + where + " order by sortid");
		//System.out.println("SESSION_CLASS_LEADER=="+super.getSession().getAttribute("SESSION_CLASS_LEADER"));
		HttpSession session= this.getSession();
		return "CYDY_POPUP_SUCCESS";		
		
	}
	/**
	 * 保存常用短语
	 * @return
	 * @throws AppException
	 */
	public String saveCydy() throws AppException {
		
		// 新建时
		if (cydy.getId() == 0) {
			cydy.setDytypename(Resources.getString(cydy.getDytype(), "struts"));
			dicService.save(cydy);	
			// 修改排序号
			dicService.updateByHQL("update TCydy set sortid = ? where id = ?", new Object[]{cydy.getId(), cydy.getId()});
			
		} else {
			
			dicService.update(cydy);
			
		}
		
		cydyList = dicService.findListByHql(" from TCydy where dytype = '" + cydy.getDytype() + "' order by sortid");
		
		super.setInfo("保存成功");
		
		return "CYDY_POPUP_SUCCESS";
	}
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String deleteCydy() throws AppException {
		
		dicService.doDelAll(id, "TCydy");
		
		//cydyList = dicService.findListByHql(" from TCydy where dytype = '" + cydy.getDytype() + "' order by sortid");
		cydyList = dicService.findListByHql(" from TCydy where dytype = '" + cydy.getDytype() + "' order by dytype, sortid");
		
		return "CYDY_POPUP_SUCCESS";
		
	}
	public DictionaryService getDicService() {
		return dicService;
	}

	public void setDicService(DictionaryService dicService) {
		this.dicService = dicService;
	}

	public TDictionary getDic() {
		return dic;
	}

	public void setDic(TDictionary dic) {
		this.dic = dic;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<TDictionary> getDicList() {
		return dicList;
	}

	public TDicType getDictype() {
		return dictype;
	}

	public void setDictype(TDicType dictype) {
		this.dictype = dictype;
	}

	public void setPagetype(String pagetype) {
		this.pagetype = pagetype;
	}

	public TCydy getCydy() {
		return cydy;
	}

	public void setCydy(TCydy cydy) {
		this.cydy = cydy;
	}

	public TCydyType getCydytype() {
		return cydytype;
	}

	public void setCydytype(TCydyType cydytype) {
		this.cydytype = cydytype;
	}

	public List<TCydy> getCydyList() {
		return cydyList;
	}

	public void setCydyList(List<TCydy> cydyList) {
		this.cydyList = cydyList;
	}

	public String getPagetype() {
		return pagetype;
	}

	public void setDicList(List<TDictionary> dicList) {
		this.dicList = dicList;
	}

}
